package com.jeespring.business.controller;

import com.jeespring.dto.BusinessDTO;
import com.jeespring.dto.ResponseDTO;
import com.jeespring.entity.Account;
import com.jeespring.entity.Order;
import com.jeespring.entity.Storage;
import com.jeespring.service.AccountService;
import com.jeespring.service.BusinessService;
import com.jeespring.service.OrderService;
import com.jeespring.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: SubmitOrder
 * @Description:
 * @Author 张朝阳
 * @Date 2020/11/29 16:06
 */
@Controller
@RequestMapping("/business")
@Slf4j
public class BusinessController {

    @Reference
    private OrderService orderService;

    @Reference
    private AccountService accountService;

    @Reference
    private StorageService storageService;

    @Autowired
    private BusinessService businessService;

    @RequestMapping("/show")
    public String show(Model model){
        List<Order> orderList = orderService.query(new Order());
        List<Account> accountList = accountService.query(new Account());
        List<Storage> storageList = storageService.query(new Storage());
        model.addAttribute("orderList",orderList);
        model.addAttribute("accountList",accountList);
        model.addAttribute("storageList",storageList);
        return "show.html";
    }

    /**
     * 模拟用户购买商品下单业务逻辑流程
     * @Param:
     * @Return:
     */
    @PostMapping("/submitOrder")
    @ResponseBody
    ResponseDTO submitOrder(@RequestBody BusinessDTO businessDTO){
        log.info("请求参数：{}",businessDTO.toString());
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            responseDTO = businessService.submitOrder(businessDTO);
        } catch (Exception e) {
            responseDTO.setSuccess(false);
            responseDTO.setMessage("提交订单失败，失败原因：" + e);
        }
        return responseDTO;
    }

    /**
     * 模拟用户购买商品下单业务逻辑流程
     * @Param:
     * @Return:
     */
    @PostMapping("/submitOrder2")
    @ResponseBody
    ResponseDTO submitOrder2(@RequestBody BusinessDTO businessDTO){
        log.info("请求参数：{}",businessDTO.toString());
        return businessService.submitOrderException(businessDTO);
    }
}
